import React, { useState } from 'react';
import { Descriptions, Divider, Button, Popconfirm } from 'antd';
import OptForm from './OptForm';
import PrintServiceRecord from './PrintServiceRecord';
import { PrinterOutlined } from '@ant-design/icons';
import { Rank } from '@/utils/constants';
import { 
  createPatientArchive,
  updatePatientArchive,
  updateWorkerOrderStatus
} from '@/services/order/api';
import { showMsg, MsgType, isExits } from '@/utils/utils';

const ArchivesInfo: React.FC<{order_id: string; detail: ORDERAPI.ArchiveInfo; onReload?: () => void;}> = (props) => {
  const { order_id, detail, onReload } = props;

  const [visible, setVisible] = useState<boolean>(false);
  const [printVisible, setPrintVisible] = useState<boolean>(false);

  /**
   * 创建
   * @param fields 
   */
   const handleAdd = async (fields: any) => {
    try {
      if (!order_id) {
        await createPatientArchive({ ...fields });
      } else {
        await updatePatientArchive({ ...fields, order_id });
      }
      showMsg(!order_id ? '添加成功' : '修改成功', MsgType.SUCCESS);
      return true;
    } catch (error) {
      showMsg(error?.data?.errmsg);
      return false;
    }
  };

  const onRefresh = () => {
    onReload && onReload();
  }

  // 打印
  const onPrint = () => {
    setPrintVisible(true);
  }

  return <div className="service-record">
     <div className="header">
        <div className="title">1.档案信息</div>
        <div className="btns">
          {
            (['P'].includes(detail.order_status as string) && isExits('400403005')) && <Popconfirm 
            placement="leftTop"
            okText="确认结单"
            onConfirm={async () => {
              const params = {
                order_id,
                status: 'F'
              };
              try {
                await updateWorkerOrderStatus(params);
                showMsg('已结单', MsgType.SUCCESS);
                onRefresh();
              } catch (error) {
                showMsg(error?.data?.errmsg);
              }
            }}
            cancelText="再想想"
            title='结单后，该档案记录将无法修改，是否继续？'>
              <Button type="primary">结单</Button>
            </Popconfirm>
          }
        </div>
      </div>
      <Divider style={{margin: '10px 0'}}/>
      <div style={{marginBottom: 10}}>
        {
          (detail.order_status === 'C' && isExits('400403006')) && <Button onClick={() => setVisible(true)} style={{marginRight: "10px"}} type="primary">编辑</Button>
        }
        {
          isExits('400403007') && <Button icon={<PrinterOutlined />} disabled={detail.order_status === 'C' || detail.order_status === 'E'} onClick={onPrint} type="primary">打印服务档案</Button>
        }
      </div>

      <Descriptions bordered className="archives-info-descriptions">
        <Descriptions.Item label="档案编号">
          {detail.order_id}
        </Descriptions.Item>
        <Descriptions.Item label="创建时间">
        {(detail.create_date || '') + ' ' + (detail.create_time || '')}
        </Descriptions.Item>
        <Descriptions.Item label="创建人">
        {detail.create_user_name || '-'}
        </Descriptions.Item>
        <Descriptions.Item label="服务方式">
        {detail.service_type === 'I' ? '院内' : '院外'}
        </Descriptions.Item>
        <Descriptions.Item label="服务内容">
        {detail.service_content === 'P' ? '陪诊' : detail.service_content === 'H' ? '护理' : ''}
        </Descriptions.Item>
        <Descriptions.Item label="服务星级">
        {Rank[detail.rank]}
        </Descriptions.Item>
        <Descriptions.Item label="住院号/身份证号">
        { (detail.admission_num || '无')+ '/' + (detail.identity_no || '无') }
        </Descriptions.Item>
        <Descriptions.Item label="姓名">
        {detail.patient_name}
        </Descriptions.Item>
        <Descriptions.Item label="服务地址">
        {detail.address}
        </Descriptions.Item>
        <Descriptions.Item label="开始时间（估）">
        {detail.begin_date}
        </Descriptions.Item>
        {/* <Descriptions.Item label="结束时间（估）">
        {detail.end_date}
        </Descriptions.Item>
        <Descriptions.Item label="护理天数">
        {detail.nurse_day}天
        </Descriptions.Item> */}
        <Descriptions.Item label="联系人">
        {detail.liaison_name || '-'}
        </Descriptions.Item>
        <Descriptions.Item span={2} label="电话">
        {detail.liaison_mobile || '-'}
        </Descriptions.Item>
        <Descriptions.Item span={3} label="备注">
        {detail.remark || '-'}
        </Descriptions.Item>
      </Descriptions>

      {
        visible && <OptForm 
          handleModalVisible={setVisible}
          handleAdd={handleAdd}
          record={detail}
          optType="edit"
          onRefresh={onRefresh}
        />
      }

      {
        printVisible && <PrintServiceRecord 
          handleModalVisible={setPrintVisible}
          detail={detail}
          order_id={order_id}
        />
      }
  </div>
}

export default ArchivesInfo;